home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / FinderGrok / osa stuff / CAEDescriptor.cp next >
Text File  |  2000-06-23  |  7KB  |  295 lines

  1. #include "CAEDescriptor.h"
  2. #include "Stringiness.h"
  3.  
  4. #include <algorithm>
  5.  
  6. CAEDescriptor::CAEDescriptor(
  7.     const CAEDescriptor& inOther)
  8. {
  9.     ThrowIfOSErr_( ::AEDuplicateDesc( &inOther.mDesc, &mDesc) );
  10. }
  11.  
  12. CAEDescriptor&
  13. CAEDescriptor::operator=(
  14.     const CAEDescriptor& rhs )
  15. {
  16.     CAEDescriptor tmp(rhs);
  17.     swap(tmp);
  18.     return *this;
  19. }
  20.  
  21. void
  22. CAEDescriptor::swap(
  23.     CAEDescriptor& inOther)
  24. {
  25.     std::swap(mDesc.descriptorType, inOther.mDesc.descriptorType);
  26.     std::swap(mDesc.dataHandle, inOther.mDesc.dataHandle);
  27. }
  28.  
  29. CAEDescriptor::CAEDescriptor(const AEDesc* inDesc)
  30. {
  31.     ThrowIfOSErr_( ::AEDuplicateDesc( inDesc, &mDesc) );
  32. }
  33.  
  34. CAEDescriptor::CAEDescriptor()
  35. {
  36.     mDesc.descriptorType = typeNull;
  37.     mDesc.dataHandle = 0;
  38. }
  39.  
  40. //    Warning:
  41. //
  42. //    This constructor will NOT fail if the indicated parameter does
  43. //    not exist.  Instead, the CAEDescriptor will be initialized to typeNull.
  44. //    This way, the caller can explicitly check to see if the indicated
  45. //    parameter was non-existent (typeNull), or just fail when trying to
  46. //    get data out of the typeNull descriptor.
  47.  
  48. CAEDescriptor::CAEDescriptor(
  49.     const AEDesc    &inDesc,
  50.     AEKeyword        inKeyword,
  51.     DescType        inDesiredType)
  52. {
  53.     mDesc.descriptorType = typeNull;
  54.     mDesc.dataHandle = 0;
  55.     
  56.     OSErr    err;
  57.     
  58.     switch (inDesc.descriptorType) {
  59.  
  60.         case typeAERecord:
  61.         case typeAppleEvent: {
  62.             err = ::AEGetParamDesc(&inDesc, inKeyword, inDesiredType, &mDesc);
  63.             //    Don't throw.
  64.             //    The typeNull value assigned above is sufficient.
  65.             break;
  66.         }
  67.         
  68.         case typeNull:
  69.             break;
  70.         
  71.         default: {
  72.             CAEDescriptor    temp;
  73.             
  74.             err = ::AECoerceDesc(&inDesc, typeAERecord, &temp.mDesc);
  75.             ThrowIfOSErr_(err);
  76.             
  77.             err = ::AEGetParamDesc(&temp.mDesc, inKeyword, inDesiredType, &mDesc);
  78.             
  79.             //    Don't throw if the descriptor doesn't exist.
  80.             //    The typeNull value assigned above is sufficient.
  81.             if (err != errAEDescNotFound)
  82.                 ThrowIfOSErr_(err);
  83.             break;
  84.         }
  85.     }
  86. }
  87.  
  88.  
  89. CAEDescriptor::CAEDescriptor(
  90.     DescType    inType,
  91.     const void    *inData,
  92.     SInt32        inSize)
  93. {
  94.     mDesc.descriptorType = typeNull;
  95.     mDesc.dataHandle = NULL;
  96.     
  97.     OSErr    err = ::AECreateDesc(inType, inData, inSize, &mDesc);
  98.     ThrowIfOSErr_(err);
  99. }
  100.  
  101.  
  102. CAEDescriptor::CAEDescriptor(
  103.     Boolean        inValue)
  104. {
  105.     mDesc.descriptorType = typeNull;
  106.     mDesc.dataHandle = NULL;
  107.     
  108.     OSErr    err = ::AECreateDesc(typeBoolean, &inValue, sizeof(inValue), &mDesc);
  109.     ThrowIfOSErr_(err);
  110. }
  111.  
  112.  
  113. CAEDescriptor::CAEDescriptor(
  114.     SInt16        inValue)
  115. {
  116.     mDesc.descriptorType = typeNull;
  117.     mDesc.dataHandle = NULL;
  118.     
  119.     OSErr    err = ::AECreateDesc(typeShortInteger, &inValue, sizeof(inValue),
  120.                                     &mDesc);
  121.     ThrowIfOSErr_(err);
  122. }
  123.  
  124.  
  125. CAEDescriptor::CAEDescriptor(
  126.     SInt32        inValue)
  127. {
  128.     mDesc.descriptorType = typeNull;
  129.     mDesc.dataHandle = NULL;
  130.     
  131.     OSErr    err = ::AECreateDesc(typeLongInteger, &inValue, sizeof(inValue),
  132.                                     &mDesc);
  133.     ThrowIfOSErr_(err);
  134. }
  135.  
  136.  
  137. CAEDescriptor::CAEDescriptor(
  138.     OSType        inValue)
  139. {
  140.     mDesc.descriptorType = typeNull;
  141.     mDesc.dataHandle = NULL;
  142.     
  143.     OSErr    err = ::AECreateDesc(typeType, &inValue, sizeof(inValue), &mDesc);
  144.     ThrowIfOSErr_(err);
  145. }
  146.  
  147.  
  148. CAEDescriptor::CAEDescriptor(
  149.     ConstStringPtr    inString)
  150. {
  151.     mDesc.descriptorType = typeNull;
  152.     mDesc.dataHandle = NULL;
  153.     
  154.     OSErr    err = ::AECreateDesc(typeChar, inString + 1, inString[0], &mDesc);
  155.     ThrowIfOSErr_(err);
  156. }
  157.  
  158. CAEDescriptor::CAEDescriptor(const FSSpec& inSpec)
  159. {
  160.     mDesc.descriptorType = typeNull;
  161.     mDesc.dataHandle = NULL;
  162.     
  163.     OSErr    err = ::AECreateDesc(typeFSS, &inSpec, sizeof(inSpec), &mDesc);
  164.     ThrowIfOSErr_(err);
  165. }
  166.  
  167.  
  168. // ---------------------------------------------------------------------------
  169. //        • ~CAEDescriptor
  170. // ---------------------------------------------------------------------------
  171. //    Destructor
  172.  
  173. CAEDescriptor::~CAEDescriptor()
  174. {
  175.     if (mDesc.dataHandle != 0) {
  176.         ::AEDisposeDesc(&mDesc);
  177.     }
  178. }
  179.  
  180. std::string CAEDescriptor::AsString() const
  181. {
  182.     CAEDescriptor r(Coerce(typeText));
  183.     return ::AsString(r.mDesc.dataHandle);
  184. }
  185.  
  186. CAEDescriptor CAEDescriptor::At(int index) const
  187. {
  188.     CAEDescriptor r;
  189.     AEKeyword theAEKeyword;
  190.     ThrowIfOSErr_(AEGetNthDesc(&mDesc, index, typeWildCard, &theAEKeyword, r));
  191.     return r;
  192. }
  193.  
  194. CAEDescriptor CAEDescriptor::Coerce(DescType desiredType) const
  195. {
  196.     if (mDesc.descriptorType == desiredType)
  197.         return *this;
  198.         
  199.     CAEDescriptor r;
  200.     OSErr err = AECoerceDesc(&mDesc, typeText, r);
  201.     if (err == noErr)
  202.         return r;
  203.     
  204.     if (err != errAECoercionFail)
  205.         Throw_(err);
  206.  
  207.     AECoercionHandlerUPP handler;
  208.     long handlerRefcon;
  209.     Boolean fromTypeIsDesc;
  210.     err = AEGetCoercionHandler(mDesc.descriptorType, desiredType, &handler, &handlerRefcon, 
  211.         &fromTypeIsDesc, true);
  212.     ThrowIfOSErr_(err);
  213.     
  214.     return *this;
  215. }
  216.  
  217. int CAEDescriptor::Count() const
  218. {
  219.     if (mDesc.descriptorType != typeAEList)
  220.         return 0;
  221.         
  222.     long theCount;
  223.     ThrowIfOSErr_(AECountItems(&mDesc, &theCount));
  224.     return theCount;
  225. }
  226.  
  227. // ---------------------------------------------------------------------------
  228. //        • GetParamDesc
  229. // ---------------------------------------------------------------------------
  230. //    Load Descriptor data from an AppleEvent
  231. //
  232. //    Throws an OSErr exception if it can't get the data
  233.  
  234. void
  235. CAEDescriptor::GetParamDesc(
  236.     const AppleEvent    &inAppleEvent,
  237.     AEKeyword            inKeyword,
  238.     DescType            inDesiredType)
  239. {
  240.     OSErr    err = ::AEGetParamDesc(&inAppleEvent, inKeyword, inDesiredType,
  241.                                     &mDesc);
  242.     ThrowIfOSErr_(err);
  243. }
  244.  
  245. // ---------------------------------------------------------------------------
  246. //        • GetOptionalParamDesc
  247. // ---------------------------------------------------------------------------
  248. //    Load optional Descriptor data from an AppleEvent
  249. //
  250. //    Differs from GetParamDesc in that it does not throw an exception
  251. //    if the request fails because the specified keyword data does not
  252. //    exist. Use this function to extract optional parameters from
  253. //    an AppleEvent.
  254.  
  255. void
  256. CAEDescriptor::GetOptionalParamDesc(
  257.     const AppleEvent&    inAppleEvent,
  258.     AEKeyword            inKeyword,
  259.     DescType            inDesiredType)
  260. {
  261.     OSErr    err = ::AEGetParamDesc(&inAppleEvent, inKeyword, inDesiredType,
  262.                                     &mDesc);
  263.     if ((err != errAEDescNotFound) && (err != noErr)) ThrowOSErr_(err);
  264. }
  265.  
  266. bool
  267. CAEDescriptor::HasData() const
  268. {
  269.     return mDesc.dataHandle != 0;
  270. }
  271.  
  272. bool
  273. CAEDescriptor::IsList() const
  274. {
  275.     return mDesc.descriptorType == typeAEList;
  276. }
  277.  
  278. CAEDescriptor CAEDescriptor::MakeList()
  279. {
  280.     CAEDescriptor r;
  281.     ThrowIfOSErr_(AECreateList(0, 0, false, r));
  282.     return r;
  283. }
  284.  
  285. void CAEDescriptor::Put(int index, DescType inType, const void *inData, SInt32 inSize)
  286. {
  287.     ThrowIfOSErr_(AEPutPtr(&mDesc, index, inType, inData, inSize));
  288. }
  289.  
  290.  
  291. void CAEDescriptor::Put(int index, const CAEDescriptor& d)
  292. {
  293.     ThrowIfOSErr_(AEPutDesc(&mDesc, index, d));
  294. }
  295.